from wormbane.model.feature import Corridor, Doorway, Hole, Pit
from wormbane.model.rock import Rock
from wormbane.model.races.troll import Troll
from wormbane.util.random_ import percent_chance

import random

def action_dig(monster, target, game):
    game.write(monster, 'digs into', target)
    monster.time += 1
    if target.is_wall or target.is_door:
        if percent_chance(50):
            cell = target.env
            game.write(target, 'shatters')
            if target.axis:
                Doorway(material = target.material,
                        axis = target.axis).move_to(cell)
            else:
                Corridor().move_to(cell)
    elif target.kind in ('corridor', 'doorway', 'floor'):
        if percent_chance(50):
            cell = target.env
            cell.feature.move_inv_to(cell)
            Pit().move_to(cell)
            game.write(monster, 'makes', cell.feature)
    elif target.is_pit:
        if percent_chance(50):
            cell = target.env
            Hole().move_to(cell)
            game.write(monster, 'makes', cell.feature)
    elif target.is_boulder:
        if target.alive:
            game.write(target, 'wakes up')
            Troll.create_equipped_monster().move_to(target.env)
            target.move_to(None)
        else:
            if percent_chance(50):
                game.write(target, 'shatters')
                target.move_inv_to(target.env)
                Rock(count = random.randint(20, 50)).move_to(target.env)
                target.move_to(None)
